<template>
    <div>
        <breadcrumb :routerList="routerList"></breadcrumb>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px"  class="demo-ruleForm">
            <el-form-item label="平台抽成比例"
                          prop="mall_royalty">
                <el-input v-model="form.mall_royalty" class="input" :disabled="isDisabled"></el-input>
            </el-form-item>
            <el-form-item label="会员抽成比例"
                          prop="user_royalty"
            >
                <el-input v-model="form.user_royalty" class="input" :disabled="isDisabled"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('form')" :disabled="isDisabled">提交</el-button>
                <el-button @click="reset">修改</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import breadcrumb from '@/components/breadcrumb.vue'
    export default {
        name: "memberRule",
        components:{breadcrumb},
        data(){
            return {
                routerList:[
                    {
                        name:'会员管理',
                        path:''
                    },
                    {
                        name:'会员机制',
                        path:'/memberRule'
                    }
                ],
                form:{
                    mall_royalty:0,
                    user_royalty:0
                },
                rules:{
                    mall_royalty:[
                        {required:true,message:'请输入平台抽成比例',trigger:'blur'},
                    ],
                    user_royalty:[
                        {required:true,message:'请输入平台抽成比例',trigger:'blur'},
                    ]
                },
                isDisabled:true
            }
        },
        created(){
            this.getMemberRule()
        },
        methods:{
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        console.log(this.form.mall_royalty);
                        this.submitMemberRule()
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            },
            async getMemberRule(){
                let res = await this.$Http.getMemberRule();
                console.log(res);
                this.form.mall_royalty = res.mall_royalty;
                this.form.user_royalty = res.user_royalty;
            },
            async submitMemberRule(){
                let res = await this.$Http.memberRule({
                    mall_royalty:this.form.mall_royalty,
                    user_royalty:this.form.user_royalty
                })
                console.log(res)
                if(res.code == 200){
                    this.$message({
                        type: 'success',
                        message: '提成比例设置成功'
                    });
                    this.isDisabled = true
                }
            },
            reset(){
                this.isDisabled = false;
            }
        }
    }
</script>

<style scoped>
    .input{
        width: 50%;
    }
    .demo-ruleForm{
        width: 100%;
        background: white;
        margin-top: 20px;
        padding: 20px 0;
        box-sizing: border-box;
    }
</style>
